/*******************************************************************************
 * Copyright (c) 2011 Irineu Martins de Lima Moura (imlm2).
 * This file is part of JBeat.
 * 
 *     JBeat is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     JBeat is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with JBeat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Irineu Martins de Lima Moura (imlm2) - initial API and implementation
 ******************************************************************************/
package br.ufpe.cin.imlm2.jbeat.soot.common;

import java.util.Collection;
import java.util.Map;

import br.ufpe.cin.imlm2.util.Parameterizable;
import br.ufpe.cin.imlm2.util.options.NoSuchOptionException;

import soot.Scene;
import soot.SceneTransformer;
import soot.SootClass;

/**
 * An abstract tagger for the Soot Scene. The general idea is that a Tagger
 * works over a set of classes (the <i>targetSet</i>) tagging either the classes
 * themselves, methods or any possible host accessible through the
 * <i>targetSet</i>.
 * 
 * @author Irineu
 * 
 */
public abstract class Tagger extends SceneTransformer implements Parameterizable{

	protected Collection<SootClass> targetSet;
	protected boolean rtLibraries;
	protected JBeatOptions options;
	
	public Tagger() {
		this.options = new JBeatOptions();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void internalTransform(String phaseName, Map options) {
		try {	
			this.options.parse(options);
		} catch (NoSuchOptionException e) {
			System.err.println("Invalid option '" + e.getInvalidOtion()
					+ "' being ignored. Reamining options being ignored");
		}
		
		if (this.options.isRt_libraries())
			this.targetSet = Scene.v().getClasses();
		else
			targetSet = Scene.v().getApplicationClasses();
		this.generateTags(this.targetSet);
		this.executePreTaggingTransformations(this.targetSet);
		this.tag(this.targetSet);
		this.executePostTaggingTransformations(this.targetSet);
	}

	protected abstract void tag(Collection<SootClass> classes);

	protected abstract void executePostTaggingTransformations(
			Collection<SootClass> classes);

	protected abstract void executePreTaggingTransformations(
			Collection<SootClass> classes);

	protected abstract void generateTags(Collection<SootClass> classes);

	@Override
	public JBeatOptions getOptions() {
		return options;
	}
	
	

}
